package homework;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class MainApp {
    public static void main(String[] args) {
        //键盘录入功能
        Scanner sc = new Scanner(System.in);
        //创建集合,存储用户
        ArrayList<User> users = new ArrayList<>();
        //用于存储数学题目
        ArrayList<MathTest> allTests = new ArrayList<>();
        //创建学生和老师对象
        users.add(new Student("哪吒", "123456"));
        users.add(new Teacher("太乙真人", "123456"));

        //测试数据
        allTests.add(new AddTest(10, 5));//添加加法题
        allTests.add(new SubtractTest(6, 3));//添加减法题
        allTests.add(new MultiplyTest(10, 2));//添加乘法题
        allTests.add(new DivideTest(8, 2));//添加除法题

        while (true) {
            System.out.println("欢迎来到黑马数学心算练习系统");

            //登录功能实现
            User loginUser = null;
            while (true) {
                System.out.println("请输入用户名：");
                String username = sc.nextLine();
                System.out.println("请输入用密码:");
                String password = sc.nextLine();

                for (int i = 0; i < users.size(); i++) {
                    User user = users.get(i);
                    if (Objects.equals(username, user.getName()) && Objects.equals(password, user.getPassword())) {
                        loginUser = user;
                    }
                }

                if (loginUser == null) {
                    System.out.println("您输入的用户名和密码不存在！请继续输入");
                } else {
                    break;
                }
            }

            if (loginUser instanceof Student) {
                showStudentUI(sc, loginUser, allTests);
            } else {
                showTeacherUI(sc, loginUser, allTests);
            }

            System.out.println("继续登录系统请输入Y，否则退出系统");
            String isContinue = sc.nextLine();
            if (!isContinue.equalsIgnoreCase("Y")) {
                System.out.println("谢谢光临！");
                break;
            }
        }
    }

    /**
     * 学生用户进入的功能界面
     *
     * @param sc
     * @param user
     * @param allTests
     */
    public static void showStudentUI(Scanner sc, User user, ArrayList<MathTest> allTests) {

        System.out.println("欢迎" + user.getName() + "同学进入系统！");

        while (true) {
            int score = answer(sc, allTests);
            System.out.println("一共" + allTests.size() + "道题，每题一分，共得分" + score);
            if (score == allTests.size()) {
                System.out.println("哇哦！算术这方面，你就是王者！佩服你");
            } else if (score < allTests.size()) {
                System.out.println("成绩不理想！");
            }

            System.out.println("成绩不服，输入Y继续挑战，否则输入其它退出！");
            String isContinue = sc.next();
            if (!isContinue.equalsIgnoreCase("Y")) {
                break;
            }
        }
    }

    /**
     * 老师用户进入的功能界面
     *
     * @param sc
     * @param user
     * @param allTests
     */
    public static void showTeacherUI(Scanner sc, User user, ArrayList<MathTest> allTests) {
        while (true) {
            System.out.println("欢迎" + user.getName() + "老师进入系统！");
            System.out.println("当前题库如下：");
            for (int i = 0; i < allTests.size(); i++) {
                MathTest mathTest = allTests.get(i);
                System.out.println("问题" + (i + 1) + ":" + mathTest.getQuestion() + "   " + "参考答案:" + mathTest.calculate());
            }


            System.out.println("是否要编辑题目,需要编辑输入Y,输入其他无需修改，并退出登录！");
            String str = sc.nextLine();
            if (!str.equalsIgnoreCase("Y")) {
                System.out.println("欢迎下次挑战");
                break;
            } else {
                System.out.println("1.添加题目");
                System.out.println("2.删除题目");
                int num = sc.nextInt();
                switch (num) {
                    case 1:
                        addTest(sc, allTests);
                        break;
                    case 2:
                        deleteTest(sc, allTests);
                        break;
                }
            }
        }
    }

    /**
     * 删除试题功能
     *
     * @param sc
     * @param allTests
     */
    public static void deleteTest(Scanner sc, ArrayList<MathTest> allTests) {

        while (true) {
            System.out.println("当前题库如下：");
            for (int i = 0; i < allTests.size(); i++) {
                MathTest mathTest = allTests.get(i);
                System.out.println("问题" + (i + 1) + ":" + mathTest.getQuestion() + "\t 参考答案：" + mathTest.calculate());
            }
            System.out.println("请输入您要删除的试题序号：");
            int index = sc.nextInt() - 1;
            if (index < 0 || index >= allTests.size()) {
                System.out.println("您输入的序号有误！请重试！");
            } else {
                MathTest removeTest = allTests.remove(index);
                System.out.println("删除成功：" + removeTest.getQuestion());
                System.out.println("继续删除么？输入Y表示继续，其他退出删除");
                String isContinue = sc.next();
                if (!isContinue.equals("Y")) {
                    //结束循环删除
                    break;
                }
            }
        }

    }

    /**
     * 添加试题功能
     *
     * @param sc
     * @param allTests
     * @return
     */
    public static void addTest(Scanner sc, ArrayList<MathTest> allTests) {

        while (true) {
            System.out.println("请选择需要添加的题型：");
            System.out.println("1:加法题");
            System.out.println("2:减法题");
            System.out.println("3:乘法题");
            System.out.println("4:除法题");
            int num = sc.nextInt();
            switch (num) {
                case 1: //加
                    System.out.println("请输入被加数：");
                    int add1 = sc.nextInt();
                    System.out.println("请输入加数：");
                    int add2 = sc.nextInt();
                    AddTest addTest1 = new AddTest(add1, add2);
                    allTests.add(addTest1);
                    System.out.println("题目:" + addTest1.getQuestion() + " 添加成功!");
                    break;
                case 2: //减
                    System.out.println("请输入被减数：");
                    int subtract1 = sc.nextInt();
                    System.out.println("请输入减数：");
                    int subtract2 = sc.nextInt();
                    SubtractTest addTest2 = new SubtractTest(subtract1, subtract2);
                    allTests.add(addTest2);
                    System.out.println("题目:" + addTest2.getQuestion() + " 添加成功!");
                    break;
                case 3: //乘
                    System.out.println("请输入被乘数：");
                    int multiplyTest1 = sc.nextInt();
                    System.out.println("请输入乘数：");
                    int multiplyTest2 = sc.nextInt();
                    MultiplyTest addTest3 = new MultiplyTest(multiplyTest1, multiplyTest2);
                    allTests.add(addTest3);
                    System.out.println("题目:" + addTest3.getQuestion() + " 添加成功!");
                    break;
                case 4: //除
                    int divide1 = 0;
                    while (true) {
                        System.out.println("请输入被除数：");
                        divide1 = sc.nextInt();
                        if (divide1 == 0) {
                            System.out.println("您的除数为0,请重新输入");
                        } else {
                            System.out.println("请输入除数：");
                            break;
                        }
                    }
                    int divide2 = sc.nextInt();
                    DivideTest addTest4 = new DivideTest(divide1, divide2);
                    allTests.add(addTest4);
                    System.out.println("题目:" + addTest4.getQuestion() + " 添加成功!");
                    break;

                default:
                    System.out.println("输入有误！");
                    break;

            }
            System.out.println("是否继续添加题目，是请按Y，否则任意输入");
            String isContinue = sc.next();
            if (!isContinue.equals("Y")) {
                break;
            }
        }
    }

    /**
     * 答题功能
     *
     * @param sc
     * @param allTests
     */
    public static int answer(Scanner sc, ArrayList<MathTest> allTests) {

        int answer = 0;//回答的答案
        int score = 0;//回答的分数
        System.out.println("请开始您的答题:");
        System.out.println("您好，一共" + allTests.size() + "道题目，请开始作答，答对一题得1分，答错不给分:");

        for (int i = 0; i < allTests.size(); i++) {
            System.out.println("请回答以下问题，机会只有一次：");
            MathTest mathTest = allTests.get(i);
            String question = mathTest.getQuestion();
            System.out.println(question);

            if (i <= allTests.size() - 2) {
                answer = sc.nextInt();
                if (answer == mathTest.calculate()) {
                    System.out.println("回答正确");
                    score++;
                } else {
                    System.out.println("回答错误");
                }

            } else if (i == allTests.size() - 1) {
                System.out.println("这是一道除法题，只要回答商就行，余数不考虑");
                answer = sc.nextInt();
                if (answer == mathTest.calculate()) {
                    System.out.println("回答正确");
                    score++;
                } else {
                    System.out.println("回答错误");
                }
            }
        }
        return score;
    }
}
